package com.bestcem.xm.survey.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.exception.BusinessException;
import com.bestcem.xm.component.mybatis.service.IXmService;
import com.bestcem.xm.survey.controller.vo.SurveyExportRecordVo;
import com.bestcem.xm.survey.entity.SurveyColumn;
import com.bestcem.xm.survey.entity.SurveyRespondent;
import com.bestcem.xm.survey.entity.SurveyRespondentData;
import com.bestcem.xm.survey.entity.SurveyRespondentExportRecord;
import com.bestcem.xm.survey.grpc.client.dto.ProjectDto;
import com.bestcem.xm.survey.grpc.client.dto.QstructsDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.util.ObjectUtils;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author guita
 * @description 针对表【ds_survey_respondentexportrecord】的数据库操作Service
 * @createDate 2023-01-09 15:15:37
 */
public interface SurveyRespondentExportRecordService extends IXmService<SurveyRespondentExportRecord> {

    SurveyRespondentExportRecord getOneRespondentexportrecord(String id);

    List<SurveyRespondentExportRecord> getRespondentExportRecordList(JSONObject args);

    SurveyExportRecordVo getExportRecordPage(@ApiIgnore Page<SurveyRespondentExportRecord> page, JSONObject args);

    Map<String, Integer> deleteOneRespondentexportrecord(String id);

    Map<String, Integer> deleteRespondentexportrecordList(JSONObject args);

    ServiceResult<Map<String, String>> respondentsV12Excel(String id, JSONObject param);

    Map<String, Object> rspdExportProgress(JSONObject param);

    ServiceResult<Object> respondentsV12Attachment(String projectId, String seq);

    void checkOrg(String orgId, String companyId);

    Map<String, Object> respondentsV1(String id, JSONObject param);

    String secondsConvert2Hms(long seconds);

    Map<String, String> parseUserAgent(String userAgent);

    Integer extParamsTypeFormat(Object dtype);

    default void getQstructQuestions(JSONObject item, Map<Integer, JSONObject> qdict, List<JSONObject> questions) {
        if (ObjectUtils.isEmpty(item) || item.isEmpty()) {
            return;
        }
        String type = item.getString("type");
        if (StringUtils.isEmpty(type)) {
            Integer gid = item.getInteger("gid");
            if (gid != null) {
                JSONObject item2 = qdict.get(gid);
                if (item2 != null) {
                    questions.add(item2);
                }
            }
        } else if ("Q".equals(type)) {
            questions.add(item);
        } else if ("G".equals(type)) {
            if (item.get("items") == null) {
                throw new BusinessException(String.format("group[gid=%s] must have :items field", item.get("gid")));
            }
            item.getJSONArray("items").forEach(it -> {
                if (it instanceof JSONObject) {
                    JSONObject item3 = (JSONObject) it;
                    getQstructQuestions(item3, qdict, questions);
                } else {
                    // items 也会有这种[3,4]
                    if (NumberUtils.isDigits(it.toString())) {
                        JSONObject item4 = qdict.get(Integer.valueOf(it.toString()));
                        if (item4 != null) {
                            questions.add(item4);
                        }
                    }
                }
            });
        }
    }

    List<Map<String, Object>> genMetaFront(List<Map<String, Object>> sub_items, SurveyRespondent rspd, List<SurveyColumn> levels_front, List<String> columns_front);

    List<Map<String, Object>> genQuestionInfo(List<Map<String, Object>> sub_items, SurveyRespondent rspd, List<SurveyColumn> columns_question, ProjectDto project, JSONObject sensitive_map, SurveyRespondentData rspd_data, List<QstructsDto> qstruct_list);

    List<Map<String, Object>> genMetaBehind(List<Map<String, Object>> sub_items, SurveyRespondent rspd, ProjectDto project, List<String> columns_behind);

    List<Map<String, Object>> genScoreByCalc(List<Map<String, Object>> sub_items, SurveyRespondentData rspd_data, List<QstructsDto> qstruct_list, List<String> columns_score);

    List<Map<String, Object>> genMember(List<Map<String, Object>> sub_items, SurveyRespondent rspd, List<String> columns_member, Map<Integer, Boolean> mdisplay_range_perm);

    List<Map<String, Object>> genExtExparams(List<Map<String, Object>> sub_items, SurveyRespondentData rspd_data, List<String> columns_ext_params, Map<String, Integer> extparameters_type_map, JSONObject enum_ext_params);

    boolean checkSurveyExistsScore(String id, int version);

    void createMemberColumns(String id);

    void createExtParamsColumns(String id);
}